package com.haiheng.voiceandbook.treeview.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.haiheng.voiceandbook.R;
import com.haiheng.voiceandbook.common.DocDto;
import com.haiheng.voiceandbook.treeview.interfaces.ItemDataClickListener;
import com.haiheng.voiceandbook.treeview.interfaces.OnScrollToListener;
import com.haiheng.voiceandbook.treeview.viewholder.BaseViewHolder;
import com.haiheng.voiceandbook.treeview.viewholder.ChildViewHolder;
import com.haiheng.voiceandbook.treeview.viewholder.ParentViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author Zheng Haibo
 * @PersonalWebsite http://www.mobctrl.net
 * @Description
 */
public class RecyclerAdapter extends RecyclerView.Adapter<BaseViewHolder> {

    private static final String TAG = "RecyclerAdapter";
    private Context mContext;
    private List<DocDto> mDataSet;
    private ItemDataClickListener listener;
    public RecyclerAdapter(Context context,ItemDataClickListener listener) {
        mContext = context;
        this.listener = listener;
        mDataSet = new ArrayList<DocDto>();
    }

    public List<DocDto> getDataSet() {
        return mDataSet;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.e(TAG, "onCreateViewHolder: " + viewType);
        View view = null;
        switch (viewType) {
            case DocDto.ITEM_TYPE_PARENT:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_recycler_parent, parent, false);
                return new ParentViewHolder(view);
            case DocDto.ITEM_TYPE_CHILD:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_recycler_child, parent, false);
                return new ChildViewHolder(view);
            default:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_recycler_parent, parent, false);
                return new ChildViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case DocDto.ITEM_TYPE_PARENT:
                Log.e(TAG, "onBindViewHolder: " + mDataSet.get(position).getTitle());
                ParentViewHolder imageViewHolder = (ParentViewHolder) holder;
                imageViewHolder.bindView(mDataSet.get(position), position,
                        listener);
                break;
            case DocDto.ITEM_TYPE_CHILD:
                ChildViewHolder textViewHolder = (ChildViewHolder) holder;
                textViewHolder.bindView(mDataSet.get(position), position,listener);
                break;
            default:
                break;
        }
    }




    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

    private int getChildrenCount(DocDto item) {
        List<DocDto> list = new ArrayList<DocDto>();
        printChild(item, list);
        return list.size();
    }

    private void printChild(DocDto item, List<DocDto> list) {
        list.add(item);
        if (item.getChildren() != null) {
            for (int i = 0; i < item.getChildren().size(); i++) {
                printChild(item.getChildren().get(i), list);
            }
        }
    }


    /**
     * 从position开始删除，删除
     *
     * @param position
     * @param itemCount 删除的数目
     */
    protected void removeAll(int position, int itemCount) {
        for (int i = 0; i < itemCount; i++) {
            mDataSet.remove(position);
        }
        notifyItemRangeRemoved(position, itemCount);
    }

    protected int getCurrentPosition(String uuid) {
        for (int i = 0; i < mDataSet.size(); i++) {
            if (uuid.equalsIgnoreCase(mDataSet.get(i).getResId())) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getItemViewType(int position) {
        return mDataSet.get(position).getType();
    }

    public void add(DocDto text, int position) {
        mDataSet.add(position, text);
        notifyItemInserted(position);
    }

    public void addAll(List<DocDto> list, int position) {

      getTreeList(list);
        //notifyItemRangeInserted(position, list.size());
    }

    /**
     * 递归
     *
     * @param list
     * @return
     */
    private List<DocDto> getTreeList(List<DocDto> list) {
        for (int i = 0; i < list.size(); i++) {
            mDataSet.add(list.get(i));
            if (list.get(i).getChildren() != null && list.get(i).getChildren().size() > 0) {
				getTreeList(list.get(i).getChildren());
            }
        }
		return mDataSet;

    }

    public void delete(int pos) {
        if (pos >= 0 && pos < mDataSet.size()) {
            if (mDataSet.get(pos).getType() == DocDto.ITEM_TYPE_PARENT
                    && mDataSet.get(pos).getExpand()) {// 父组件并且子节点已经展开
                for (int i = 0; i < mDataSet.get(pos).getChildren().size() + 1; i++) {
                    mDataSet.remove(pos);
                }
                notifyItemRangeRemoved(pos, mDataSet.get(pos).getChildren()
                        .size() + 1);
            } else {// 孩子节点，或没有展开的父节点
                mDataSet.remove(pos);
                notifyItemRemoved(pos);
            }
        }
    }
}
